home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / cclib / source / loc.c < prev    next >
C/C++ Source or Header  |  1995-11-16  |  4KB  |  185 lines

  1. /*-------------------------------------------------------------------------+
  2.  |                                       |
  3.  | Name:    loc                                |
  4.  | Purpose: counts lines of actual code and comments and prints stats       |
  5.  |                                       |
  6.  | Author:  Robert W. Albrecht               Date: 9/89           |
  7.  +-------------------------------------------------------------------------*/
  8.  
  9. #define ANSIC 1
  10. #include "stdio.h"
  11. #include "ccfunc.h"
  12.  
  13. long whitechars, codechars, commentchars, codelines;
  14. long whitechars_total, codechars_total, commentchars_total, codelines_total;
  15. long blanklines, blanklines_total, filecount;
  16. short quiet;
  17. short _math = 0; /* <= don't load MathIeeeDoubBas.library */
  18.  
  19. void do_totals(void) /* add up total stats */
  20. {
  21. whitechars_total += whitechars;
  22. codechars_total += codechars;
  23. commentchars_total += commentchars;
  24. codelines_total += codelines;
  25. blanklines_total += blanklines;
  26. blanklines = whitechars = codechars = commentchars = codelines = 0L;
  27. }
  28.  
  29. void printtotals(void) /* print total stats */
  30. {
  31. long avglines;
  32. void printstats(void);
  33.  
  34. avglines = codelines_total/filecount;
  35.  
  36. printf("\nTotal statistics for %ld files...\n",filecount);
  37. whitechars = whitechars_total;
  38. codechars = codechars_total;
  39. commentchars = commentchars_total;
  40. codelines = codelines_total;
  41. blanklines = blanklines_total;
  42. printstats();
  43. printf("The average number of code lines per file is %ld.\n\n",avglines);
  44. }
  45.  
  46. void printstats(void) /* print stats */
  47. {
  48. long avgline;
  49. long code_p;
  50. long code_pn;
  51.  
  52. if( codelines )
  53.    avgline = codechars/codelines;
  54. else
  55.    avgline = 0;
  56. code_pn = code_p = codechars*100;
  57. if( commentchars + whitechars + codechars )
  58.    code_p /= (commentchars+whitechars+codechars);
  59. else
  60.    code_p = 0;
  61. if( whitechars + codechars )
  62.    code_pn /= (whitechars+codechars);
  63. else
  64.    code_pn = 0;
  65.  
  66. printf("%ld lines of code, ",codelines);
  67. printf("%ld blank lines, ",blanklines);
  68. printf("%ld bytes of code,\n",codechars);
  69. printf("%ld bytes of whitespace, ",whitechars);
  70. printf("and %ld bytes of comments.\n",commentchars);
  71. printf("The average code line has %ld bytes.\n",avgline);
  72. printf("The code content is %ld%%, %ld%% without comments.\n",
  73.    code_p, code_pn);
  74. }
  75.  
  76. #define COMMENT 1
  77. #define BLANK 1
  78. #define CODE 0
  79.  
  80. void loc(char *fname)  /* counts lines of actual code */
  81. {
  82. register FILE *f;
  83. register short c, oldc;
  84. register short mode, hascode;
  85.  
  86. if( f = fopen(fname,"r") )
  87.    {
  88.    c = oldc = -1;
  89.    mode = CODE;
  90.    hascode = BLANK;
  91.  
  92.    while( (c = getc(f)) != EOF )
  93.       {
  94.       switch( mode )
  95.      {
  96.      case CODE:
  97.         {
  98.         if( c == '*' && oldc == '/' )
  99.            {
  100.            mode = COMMENT;
  101.            codechars--;
  102.            }
  103.         else switch(c)
  104.            {
  105.            case '\n':
  106.           if( hascode == CODE )
  107.              {
  108.              codelines++;
  109.              hascode = BLANK;
  110.              }
  111.           else
  112.              blanklines++;
  113.            case '\t': case '\v': case '\b':
  114.            case '\r': case '\f': case '\a':
  115.            case ' ':
  116.           whitechars++;
  117.            break;
  118.  
  119.            default:
  120.           hascode = CODE;
  121.           codechars++;
  122.            break;
  123.            }
  124.         }
  125.      break;
  126.  
  127.      case COMMENT:
  128.         if( c == '/' && oldc == '*' )
  129.            mode = CODE;
  130.         else
  131.            commentchars++;
  132.      break;
  133.      }
  134.       oldc = c;
  135.       }
  136.    if( !quiet )
  137.       {
  138.       printf("\nThe file %s contains...\n",fname);
  139.       printstats();
  140.       }
  141.    do_totals();
  142.    filecount++;
  143.    fclose(f);
  144.    }
  145. else
  146.    printf("Can't open input file %s\n",fname);
  147. }
  148.  
  149.  
  150.  
  151. void main(long argc,char *argv[]) /* main routine */
  152. {
  153. short i;
  154. char *ptr, *scdir();
  155.  
  156. if( argc > 1 )
  157.    {
  158.    printf("Lines Of C (LOC) by Robert W. Albrecht\n\n");
  159.  
  160.    for( i = 1; i < argc; i++)
  161.       {
  162.       if( argv[i][0] == '-' )
  163.      {
  164.      if( argv[i][1] == 'Q' || argv[i][1] == 'q' )
  165.         quiet = 1;
  166.      }
  167.       else
  168.      {
  169.      while( ptr = scdir(argv[i]) )
  170.         {
  171.         loc(ptr);
  172.         }
  173.      }
  174.       }
  175.    if( filecount > 1 || quiet )
  176.       printtotals();
  177.    }
  178. else if( argc == 1 )
  179.    {
  180.    printf("Lines Of C (LOC) by Robert W. Albrecht\n\n");
  181.    printf("SYNTAX: loc [-Q] file1 file2 ...\n");
  182.    printf("Wild-Card characters accepted.\n");
  183.    }
  184. }
  185.